import {mutableHandlers, readonlyHandlers, shallowReadonlyHandlers} from "./baseHandles";
// @ts-ignore
import {isObject} from "../shared";

export const enum ReactiveFlags {
    IS_REACTIVE = '__is_Reactive',
    IS_READONLY = '__is_Readonly',
}


export function reactive(raw) {
    return new Proxy(raw, mutableHandlers)
}

export function readonly(raw) {
    return createReactiveObject(raw, readonlyHandlers)
}

export function shallowReadonly(raw) {
    return createReactiveObject(raw, shallowReadonlyHandlers)
}

export function createReactiveObject(target, baseHandlers) {
    if (!isObject(target)) {
        console.log(`target ${target} 必须是一个对象`)
        return target;
    }
    return new Proxy(target, baseHandlers);
}

export function isReactive(value) {
    return !!value[ReactiveFlags.IS_REACTIVE]
}

export function isReadonly(value) {
    return !!value[ReactiveFlags.IS_READONLY];
}

export function isProxy(value) {
    return isReadonly(value) || isReactive(value);
}
